﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;
using BinAnalysisHelp;

namespace XChipTool
{
    public partial class XChip_TveSizeReckon : DevExpress.XtraEditors.XtraUserControl
    {
        //串口
        public SerialPort sPortTemp;
        private List<tveModel> tveList;
        /// <summary>
        /// 初始化生成参数
        /// </summary>
        public List<tveModel> TveList
        {
            get
            {
                if (null == tveList)
                {
                    tveList = new List<tveModel>();
                    tveList.Add(new tveModel() { tKey = "0xfffd", tVal = "80" });
                    tveList.Add(new tveModel() { tKey = "0xfffe", tVal = "25" });

                    tveList.Add(new tveModel() { tKey = "0xc02a", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc02b", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc02c", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc02d", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc02e", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc030", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc031", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc032", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc034", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc035", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc036", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc037", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc038", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc039", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc03a", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc03b", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc03c", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc03d", tVal = "0x00" });

                    tveList.Add(new tveModel() { tKey = "0xc04e", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc050", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc051", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc052", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc053", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc054", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc055", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc056", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc057", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc058", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc059", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc046", tVal = "0x00" });
                    tveList.Add(new tveModel() { tKey = "0xc047", tVal = "0x00" });
                }

                return tveList;
            }

            set
            {
                tveList = value;
            }
        }

        public XChip_TveSizeReckon()
        {
            InitializeComponent();
        }
        public XChip_TveSizeReckon(SerialPort _sPortTemp)
        {
            sPortTemp = _sPortTemp;
            InitializeComponent();
        }
        /// <summary>
        /// 生成参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            int htsNo = SetHts(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16));
            int vtsNo = SetVts(Convert.ToInt32(V_totaltxt.Text, 16));
            int vaasNo = VActiveAreaStart(isCvbsRadio.Checked, Convert.ToInt32(V_Sizetxt.Text, 16), Convert.ToInt32(V_totaltxt.Text,16), Convert.ToInt32(VDirectionTranslationtxt.Text, 16));
            int vaaeNo = VActiveAreaEnd(Convert.ToInt32(VDirectionTranslationtxt.Text, 16), Convert.ToInt32(V_totaltxt.Text, 16));
            int vvasNo = VValidAreaStart(isCvbsRadio.Checked, Convert.ToInt32(V_Sizetxt.Text, 16), Convert.ToInt32(V_totaltxt.Text, 16), Convert.ToInt32(VDirectionTranslationtxt.Text, 16));
            int vvaeNo = VValidAreaEnd(Convert.ToInt32(VDirectionTranslationtxt.Text, 16), Convert.ToInt32(V_totaltxt.Text, 16));
            int haasNo = HActiveAreaStart(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16), Convert.ToInt32(H_Sizetxt.Text, 16), Convert.ToInt32(HDirectionStretchingtxt.Text, 16));
            int haaeNo = HActiveAreaEnd(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16));
            int hvasNo = HValidAreaStart(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16), Convert.ToInt32(H_Sizetxt.Text, 16), Convert.ToInt32(HDirectionStretchingtxt.Text, 16));
            int hvaeNo = HValidAreaEnd(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16));
            int hovsNo = HrefOutputVStart(isCvbsRadio.Checked, VActiveAreaStart(isCvbsRadio.Checked, Convert.ToInt32(V_Sizetxt.Text, 16), Convert.ToInt32(V_totaltxt.Text, 16), Convert.ToInt32(VDirectionTranslationtxt.Text, 16)));
            int hoveNo = HrefOutputVEnd(isCvbsRadio.Checked, VActiveAreaEnd(Convert.ToInt32(VDirectionTranslationtxt.Text, 16), Convert.ToInt32(V_totaltxt.Text, 16)));
            int hohsNo = HrefOutputHStart(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16), Convert.ToInt32(H_Sizetxt.Text, 16), Convert.ToInt32(HDirectionFillingtxt.Text, 16));
            int hoheNo = HrefOutputHEnd(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16), Convert.ToInt32(HDirectionFillingtxt.Text, 16));
            int tsshcNo = TveStartSyncHCounter(is720radio.Checked, Convert.ToInt32(H_totaltxt.Text, 16), Convert.ToInt32(H_Sizetxt.Text, 16));
            int tssvcNo = TveStartSyncVCounter(isCvbsRadio.Checked, VActiveAreaStart(isCvbsRadio.Checked, Convert.ToInt32(V_Sizetxt.Text, 16), Convert.ToInt32(V_totaltxt.Text, 16), Convert.ToInt32(VDirectionTranslationtxt.Text, 16)));
            htslab.Text = $"hts    {htsNo}";
            vtslab.Text = $"vts    {vtsNo}";
            vaasLab.Text = $"V active area start    {vaasNo}";
            vaaeLab.Text = $"V active area end    {vaaeNo}";
            vvasLab.Text = $"V valid area start    {vvasNo}";
            vvaeLab.Text = $"V valid area end    {vvaeNo}";
            haasLab.Text = $"H active area start    {haasNo}";
            haaeLab.Text = $"H active area end    {haaeNo}";
            hvasLab.Text = $"H valid area start    {hvasNo}";
            hvaeLab.Text = $"H valid area end    {hvaeNo}";

            hovsLab.Text = $"href output V start    {hovsNo}";
            hoveLab.Text = $"href output V end    {hoveNo}";
            hohsLab.Text = $"href output H start    {hohsNo}";
            hoheLab.Text = $"href output H end    {hoheNo}";
            tsshcLab.Text = $"tve start sync h counter    {tsshcNo}";
            tssvcLab.Text = $"tve start sync v counter    {tssvcNo}";
            FrontporchlengthLab.Text = $"Front porch length    {Convert.ToInt32(textEdit1.Text,16)}";

            TveList.ForEach(c => {
                if (c.tKey.Contains("c02a"))
                {
                    c.tVal = (htsNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c02b"))
                {
                    c.tVal = (htsNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c02c"))
                {
                    c.tVal = (vtsNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c02d"))
                {
                    c.tVal = (vtsNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c02e"))
                {
                    c.tVal = vaasNo.ToString("X2");
                }
                else if (c.tKey.Contains("c030"))
                {
                    c.tVal = (vaaeNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c031"))
                {
                    c.tVal = (vaaeNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c032"))
                {
                    c.tVal = vvasNo.ToString("X2");
                }
                else if (c.tKey.Contains("c034"))
                {
                    c.tVal = (vvaeNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c035"))
                {
                    c.tVal = (vvaeNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c036"))
                {
                    c.tVal = (haasNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c037"))
                {
                    c.tVal = (haasNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c038"))
                {
                    c.tVal = (haaeNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c039"))
                {
                    c.tVal = (haaeNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c03a"))
                {
                    c.tVal = (hvasNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c03b"))
                {
                    c.tVal = (hvasNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c03c"))
                {
                    c.tVal = (hvaeNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c03d"))
                {
                    c.tVal = (hvaeNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c04e"))
                {
                    c.tVal = hovsNo.ToString("X2");
                }
                else if (c.tKey.Contains("c050"))
                {
                    c.tVal = (hoveNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c051"))
                {
                    c.tVal = (hoveNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c052"))
                {
                    c.tVal = (hohsNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c053"))
                {
                    c.tVal = (hohsNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c054"))
                {
                    c.tVal = (hoheNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c055"))
                {
                    c.tVal = (hoheNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c056"))
                {
                    c.tVal = (tsshcNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c057"))
                {
                    c.tVal = (tsshcNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c058"))
                {
                    c.tVal = (tssvcNo % 256).ToString("X2");
                }
                else if (c.tKey.Contains("c059"))
                {
                    c.tVal = (tssvcNo / 256).ToString("X2");
                }
                else if (c.tKey.Contains("c046"))
                {
                    c.tVal = (Convert.ToInt32(textEdit1.Text,16) & 0xff).ToString("X2");
                }
                else if (c.tKey.Contains("c047"))
                {
                    c.tVal = (Convert.ToInt32(textEdit1.Text, 16) >> 8 & 0x0f).ToString("X2");
                }

            });
            StringBuilder sb = new StringBuilder();
            foreach (var item in TveList)
            {
                sb.AppendLine($"{item.tKey.ToUpper()},0x{item.tVal.ToUpper()},");
            }

            tveListlab.Text = sb.ToString();


        }
        /// <summary>
        /// 导入参数 逆推尺寸信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            OpenFileDialog sfd = new OpenFileDialog();
            //设置文件类型 
            sfd.Filter = "Tve文件（*.txt）|*.txt";
            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                var list = System.IO.File.ReadAllLines(sfd.FileName);
                tveList = null;
                List<string> addrlist = new List<string>();
                foreach (var item in list)
                {
                    string[] col = item.Replace('，', ',').Split(',');
                    if (col.Count() > 2)
                    {
                        string colFirst = col.First().Trim().ToLower();
                        addrlist.Add(colFirst);
                        TveList.ForEach(c =>
                        {
                            if (c.tKey.Trim().ToLower() == colFirst)
                            {
                                c.tVal = col[1].Replace("0x", "");
                            }
                        });
                    }
                }
                string str = "";
                foreach (var items in tveList)
                {
                    if (!addrlist.Any(c=>c.Trim().ToLower() == items.tKey.Trim().ToLower()))
                    {
                        if (!str.Contains("缺少以下寄存器："))
                        {
                            str += "\r\t缺少以下寄存器：";
                        }
                        str += items.tKey.Trim().ToLower() + ",";
                    }
                }
                    //H_total = c02b / c02a
                var c02a = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc02a").tVal, 16);
                var c02b = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc02b").tVal, 16);

                var c02c = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc02c").tVal, 16);
                var c02d = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc02d").tVal, 16);

                var c02e = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc02e").tVal, 16);
                var c030 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc030").tVal, 16);
                var c031 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc031").tVal, 16);
                //var c032 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc032").tVal, 16);
                //计算V平移=C02D/C02C-C031/C030   F19-F21
                var f19 = (c02d * 256) + c02c;
                var f21 = (c031 * 256) + c030;
                VDirectionTranslationtxt.Text = (f19 - f21).ToString("X2");
                //计算H填充=C055/54-C039/38   F25-F32
                var c038 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc038").tVal, 16);
                var c039 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc039").tVal, 16);
                var f25 = (c039 * 256) + c038;
                var c054 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc054").tVal, 16);
                var c055 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc055").tVal, 16);
                var f32 = (c055 * 256) + c054;
                HDirectionFillingtxt.Text = (f25 - f32).ToString("X2");
                //计算H拉伸=C053/C052-C037/36   F31-F24+M20
                var c052 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc052").tVal, 16);
                var c053 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc053").tVal, 16);
                var f31 = (c053 * 256) + c052;
                var c036 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc036").tVal, 16);
                var c037 = Convert.ToInt32(TveList.Find(c => c.tKey == "0xc037").tVal, 16);
                var f24 = (c037 * 256) + c036;
                HDirectionStretchingtxt.Text = (f31 - f24 + Convert.ToInt32(HDirectionFillingtxt.Text,16)).ToString("X2");
               // IF(F10 = "是", F25 / 2 - (F24 + C16) / 4, F25 - (F24 + C16) / 2)


                if (is720radio.Checked)
                {
                    H_totaltxt.Text = ((c02b * 256 + c02a) / 2).ToString("X2");
                    //(c031*256)+c030
                   
                    H_Sizetxt.Text = ((f25 / 2) - (f24 + Convert.ToInt32(HDirectionStretchingtxt.Text,16)) / 4).ToString("X2");
                }
                else
                {
                    H_totaltxt.Text = (c02b * 256 + c02a).ToString("X2");
                   
                    H_Sizetxt.Text = (f25 - (f24 + Convert.ToInt32(HDirectionStretchingtxt.Text,16)) / 2).ToString("X2");
                }
                V_Sizetxt.Text = ((f21) - c02e - Convert.ToInt16(isCvbsRadio.Checked)).ToString("X2");
                V_totaltxt.Text = (c02d * 256 + c02c).ToString("X2");
                textEdit1.Text = ((Convert.ToInt32(TveList.Find(c => c.tKey == "0xc047").tVal, 16) << 8 & 0xf00 ) + Convert.ToInt32(TveList.Find(c => c.tKey == "0xc046").tVal, 16)).ToString("X2");
                MessageBox.Show("导入完成!"+ str);
                simpleButton1_Click(null,null);
            }
        }

        int  SetHts(bool is720,int H_total)
        {
            return is720 ? H_total * 2 : H_total;
        }

        int SetVts(int V_total)
        {
            return V_total;
        }
        int VActiveAreaStart(bool isCvbs, int V_Size,int V_total,int VDirectionTranslation)
        {
            return V_total - V_Size - VDirectionTranslation - Convert.ToInt16(isCvbs);
        }
        int VActiveAreaEnd(int VDirectionTranslation,int V_total)
        {
            return V_total - VDirectionTranslation;
        }
        int VValidAreaStart(bool isCvbs, int V_Size, int V_total, int VDirectionTranslation)
        {
            return V_total- V_Size- VDirectionTranslation - Convert.ToInt16(isCvbs);
        }
        int VValidAreaEnd(int VDirectionTranslation, int V_total)
        {
            return V_total - VDirectionTranslation;
        }
        int HActiveAreaStart(bool is720,int H_total,int H_Size,int HDirectionStretching)
        {
            return is720 ? (4 * (H_total - H_Size) - HDirectionStretching) : (2 * (H_total - H_Size) - HDirectionStretching);
        }
        int HActiveAreaEnd(bool is720, int H_total)
        {
            return is720 ? H_total * 2 : H_total;
        }
        int HValidAreaStart(bool is720, int H_total, int H_Size, int HDirectionStretching)
        {
            return is720 ? (4 * (H_total - H_Size) - HDirectionStretching) : (2 * (H_total - H_Size) - HDirectionStretching);
        }
        int HValidAreaEnd(bool is720, int H_total)
        {
            return is720 ? H_total * 2 : H_total;
        }
        int HrefOutputVStart(bool isCvbs, int VActiveAreaStart)
        {
            return isCvbs ? VActiveAreaStart : VActiveAreaStart*2;
        }
        int HrefOutputVEnd(bool isCvbs, int VActiveAreaEnd)
        {
         return isCvbs ? VActiveAreaEnd : VActiveAreaEnd * 2;
        }
        int HrefOutputHStart(bool is720, int H_total, int H_Size, int HDirectionFilling)
        {
            return is720 ? (4 * (H_total - H_Size) - HDirectionFilling) : (2 * (H_total - H_Size) - HDirectionFilling);
        }
        int HrefOutputHEnd(bool is720, int H_total, int HDirectionFilling)
        {
            return (is720 ? H_total * 2 : H_total) - HDirectionFilling;
        }
        int TveStartSyncHCounter(bool is720, int H_total, int H_Size)
        {
            return is720 ? 4 * (H_total - H_Size) - 16 : 2 * (H_total - H_Size) - 32;
        }
        int TveStartSyncVCounter(bool isCvbs, int VActiveAreaStart)
        {
            return isCvbs ? VActiveAreaStart : VActiveAreaStart * 2;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tveListlab.Text))
            {
                //SerialSend.isDebug = false;
                SaveFileDialog sfd = new SaveFileDialog();
                //设置文件类型 
                sfd.Filter = "Tve文件（*.txt）|*.txt";

                //设置默认文件类型显示顺序 
                sfd.FilterIndex = 1;

                //保存对话框是否记忆上次打开的目录 
                sfd.RestoreDirectory = true;
                sfd.FileName = "Tve_Size" + DateTime.Now.ToString("yyyyMMddHHmmss");
                if (sfd.ShowDialog() == DialogResult.OK) {
                    System.IO.File.WriteAllText(sfd.FileName, tveListlab.Text);
                }
            }
            else
            {
                MessageBox.Show("无数据！请先生成数据！");
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            uint temp = (uint)SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
            if (null != BinHelp.lspList)
            {
                var lspModel = BinHelp.lspList.Find(c => c.SettingHex == temp);
                if (lspModel.tName.ToLower() == "cvbs")
                {
                    isCvbsRadio.Checked = true;
                }
                else
                {
                    isCvbsRadio.Checked = false;
                }
                if (lspModel.vName.ToLower().Contains("720p"))
                {
                    is720radio.Checked = true;
                }
                else
                {
                    is720radio.Checked = false;
                }
            }

            var L9 = (int)SerialSend.ReadRegister(0x8025c02a, sPortTemp);
            var L10 = (int)SerialSend.ReadRegister(0x8025c02b, sPortTemp);
            var L11 = (int)SerialSend.ReadRegister(0x8025c02c, sPortTemp);
            var L12 = (int)SerialSend.ReadRegister(0x8025c02d, sPortTemp);
            var L13 = (int)SerialSend.ReadRegister(0x8025c02e, sPortTemp);
            var L14 = (int)SerialSend.ReadRegister(0x8025c030, sPortTemp);
            var L15 = (int)SerialSend.ReadRegister(0x8025c031, sPortTemp);
            var L16 = (int)SerialSend.ReadRegister(0x8025c032, sPortTemp);
            var L17 = (int)SerialSend.ReadRegister(0x8025c033, sPortTemp);
            var L18 = (int)SerialSend.ReadRegister(0x8025c034, sPortTemp);
            var L19 = (int)SerialSend.ReadRegister(0x8025c035, sPortTemp);
            var L20 = (int)SerialSend.ReadRegister(0x8025c036, sPortTemp);
            var L21 = (int)SerialSend.ReadRegister(0x8025c037, sPortTemp);
            var L22 = (int)SerialSend.ReadRegister(0x8025c038, sPortTemp);
            var L23 = (int)SerialSend.ReadRegister(0x8025c039, sPortTemp);
            var L24 = (int)SerialSend.ReadRegister(0x8025c03a, sPortTemp);
            var L25 = (int)SerialSend.ReadRegister(0x8025c03b, sPortTemp);
            var L26 = (int)SerialSend.ReadRegister(0x8025c03d, sPortTemp);
            var L28 = (int)SerialSend.ReadRegister(0x8025c04e, sPortTemp);
            var L29 = (int)SerialSend.ReadRegister(0x8025c050, sPortTemp);
            var L30 = (int)SerialSend.ReadRegister(0x8025c051, sPortTemp);
            var L31 = (int)SerialSend.ReadRegister(0x8025c052, sPortTemp);
            var L32 = (int)SerialSend.ReadRegister(0x8025c053, sPortTemp);
            var L33 = (int)SerialSend.ReadRegister(0x8025c054, sPortTemp);
            var L34 = (int)SerialSend.ReadRegister(0x8025c055, sPortTemp);
            var L35 = (int)SerialSend.ReadRegister(0x8025c056, sPortTemp);
            var L36 = (int)SerialSend.ReadRegister(0x8025c057, sPortTemp);
            var L37 = (int)SerialSend.ReadRegister(0x8025c058, sPortTemp);
            var L38 = (int)SerialSend.ReadRegister(0x8025c059, sPortTemp);
            var L39 = (int)SerialSend.ReadRegister(0x8025c046, sPortTemp);
            var L40 = (int)SerialSend.ReadRegister(0x8025c047, sPortTemp);
            var H_total = 0;
            if (is720radio.Checked)
            {
                H_total = (L10 * 256 + L9) / 2;
            }
            else
            {
                H_total = L10 * 256 + L9;
            }
            var V_tota = L12 * 256 + L11;
            var V_size = 0;
            if (isCvbsRadio.Checked)
            {
                V_size = Convert.ToInt32((L15.ToString("X2") + L14.ToString("X2")), 16) - L13 - 1;
            }
            else
            {
                V_size = Convert.ToInt32((L15.ToString("X2") + L14.ToString("X2")), 16) - L13;
            }

            var VDirectionTranslation = V_tota - Convert.ToInt32((L15.ToString("X2") + L14.ToString("X2")), 16);
            var HDirectionFilling = Convert.ToInt32((L23.ToString("X2") + L22.ToString("X2")), 16) - Convert.ToInt32((L34.ToString("X2") + L33.ToString("X2")), 16);
            var HDirectionStretching = Convert.ToInt32((L32.ToString("X2") + L31.ToString("X2")), 16) - Convert.ToInt32((L21.ToString("X2") + L20.ToString("X2")), 16) + HDirectionFilling;
            var H_size = 0;
            if (is720radio.Checked)
            {
                H_size = Convert.ToInt32((L23.ToString("X2") + L22.ToString("X2")), 16) / 2 -((Convert.ToInt32((L21.ToString("X2") + L20.ToString("X2")), 16)+ HDirectionStretching) /4);
            }
            else
            {
                H_size = Convert.ToInt32((L23.ToString("X2") + L22.ToString("X2")), 16)  - ((Convert.ToInt32((L21.ToString("X2") + L20.ToString("X2")), 16) + HDirectionStretching) / 2); 
            }
            H_totaltxt.Text = H_total.ToString("X2");
            V_totaltxt.Text = V_tota.ToString("X2");
            HDirectionStretchingtxt.Text = HDirectionStretching.ToString("X2");
            HDirectionFillingtxt.Text = HDirectionFilling.ToString("X2");
            VDirectionTranslationtxt.Text = VDirectionTranslation.ToString("X2");
            V_Sizetxt.Text = V_size.ToString("X2");
            H_Sizetxt.Text = H_size.ToString("X2");
            textEdit1.Text = ((L40 << 8 & 0x0f00) + L39).ToString("X2");
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (tveList != null && tveList.Count != 0)
            {
                string fffd = "";
                string fffe = "";
                for (int i = 0; i < tveList.Count(); i++)
                {
                    if (tveList[i].tKey == "0xfffd")
                    {
                        fffd = tveList[i].tVal;
                    }
                    else if (tveList[i].tKey == "0xfffe")
                    {
                        fffe = tveList[i].tVal;
                    }
                    else if (!string.IsNullOrEmpty(tveList[i].tKey) && !string.IsNullOrEmpty(tveList[i].tVal))
                    {
                        SerialSend.WriteRegister(Convert.ToUInt32(fffd.Replace("0x", "") + fffe.Replace("0x", "") + tveList[i].tKey.Replace("0x", ""), 16), Convert.ToUInt32(tveList[i].tVal, 16), sPortTemp);
                    }
                }
            }
            
        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit1.CheckState == CheckState.Checked)
            {
                SerialSend.WriteRegister(0x8030001a, 0x81, sPortTemp);
            }
            else
            {
                SerialSend.WriteRegister(0x8030001a, 0x00, sPortTemp);
            }
            
        }
    }


    public class tveModel
    {
        /// <summary>
        /// 地址
        /// </summary>
        public string tKey { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string tVal { get; set; }
        /// <summary>
        /// 注释
        /// </summary>
        public string tNode { get; set; }
        /// <summary>
        /// 原始数据
        /// </summary>
        public string tOriginal { get; set; }
    }
}
